﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Calculator
{
    public partial class Form1 : Form
    {
        private StringBuilder inputStringBuilder = new StringBuilder(); // 用于存储用户的输入
        
        public Form1()
        {
            InitializeComponent();
        }

        private void num_1_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            string digit = button.Text;
            inputStringBuilder.Append(digit);
            textBox.Text = inputStringBuilder.ToString();
        }


        private void num_2_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            string digit = button.Text;
            inputStringBuilder.Append(digit);
            textBox.Text = inputStringBuilder.ToString();
        }

        private void num_3_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            string digit = button.Text;
            inputStringBuilder.Append(digit);
            textBox.Text = inputStringBuilder.ToString();
        }

        private void num_4_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            string digit = button.Text;
            inputStringBuilder.Append(digit);
            textBox.Text = inputStringBuilder.ToString();
        }

        private void num_5_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            string digit = button.Text;
            inputStringBuilder.Append(digit);
            textBox.Text = inputStringBuilder.ToString();
        }

        private void num_6_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            string digit = button.Text;
            inputStringBuilder.Append(digit);
            textBox.Text = inputStringBuilder.ToString();
        }

        private void num_7_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            string digit = button.Text;
            inputStringBuilder.Append(digit);
            textBox.Text = inputStringBuilder.ToString();
        }

        private void num_8_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            string digit = button.Text;
            inputStringBuilder.Append(digit);
            textBox.Text = inputStringBuilder.ToString();
        }

        private void num_9_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            string digit = button.Text;
            inputStringBuilder.Append(digit);
            textBox.Text = inputStringBuilder.ToString();
        }

        private void num0_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            string digit = button.Text;
            inputStringBuilder.Append(digit);
            textBox.Text = inputStringBuilder.ToString();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (inputStringBuilder.Length > 0)
            {
                inputStringBuilder.Append("@");
                textBox.Text = inputStringBuilder.ToString();
            }
        }

        //private void btnDle_Click(object sender, EventArgs e)
        //{
        //    if (inputStringBuilder.Length > 0)
        //    {
        //        inputStringBuilder.Append("-");
        //        textBox.Text = inputStringBuilder.ToString();
        //    }
        //}

        private void btnEql_Click(object sender, EventArgs e)
        {
            if (inputStringBuilder.Length > 0)
            {
                // 解析用户输入的表达式并计算结果
                string expression = inputStringBuilder.ToString();
                double result = Calculate(expression);

                // 显示结果
                textBox.Text = result.ToString();
            }
        }

        public double Calculate(string expression)
        {
            //DataTable dataTable = new DataTable();
            //var result = dataTable.Compute(expression, "");
            //return int.Parse(result.ToString());
            // 在@符号处分割字符串
            string[] parts = expression.Split('@');

            if (parts.Length == 2)
            {
                // 提取@前后的两个数字
                string number1 = parts[0];
                string number2 = parts[1];

                // 将字符串数字解析为实际数字类型（例如 double）
                double operand1 = double.Parse(number1);
                double operand2 = double.Parse(number2);

                // 执行自定义运算（例如相加）
                double result = operand1 / (operand2 * 0.87);

                return result;
            }
            else
            {
                return 0;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // 重置输入
            inputStringBuilder.Clear();
            textBox.Text = inputStringBuilder.ToString();
        }

        private void dot_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;
            string digit = button.Text;
            inputStringBuilder.Append(digit);
            textBox.Text = inputStringBuilder.ToString();
        }
    }
}
